package com.tiny.tinyweb.core.jsf;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tiny.tinyweb.core.utils.FacesUtils;

public abstract class AbstractPageBean {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    public void beforePhase(PhaseEvent event) {
        PhaseId phaseId = event.getPhaseId();
        logger.info("BEFORE PHASE: " + phaseId.toString());
        if (PhaseId.RESTORE_VIEW.equals(phaseId)) {
            beforeRestoreView();
        } else if (PhaseId.APPLY_REQUEST_VALUES.equals(phaseId)) {
            beforeApplyRequestValues();
        } else if (PhaseId.PROCESS_VALIDATIONS.equals(phaseId)) {
            beforeProcessValidations();
        } else if (PhaseId.UPDATE_MODEL_VALUES.equals(phaseId)) {
            beforeUpdateModelValues();
        } else if (PhaseId.INVOKE_APPLICATION.equals(phaseId)) {
            beforeInvokeApplication();
        } else if ((PhaseId.RENDER_RESPONSE.equals(phaseId))
                && (!FacesContext.getCurrentInstance().getResponseComplete())) {
            beforeRenderResponse();
        }
    }

    public void afterPhase(PhaseEvent event) {
        PhaseId phaseId = event.getPhaseId();
        logger.info("AFTER PHASE: " + phaseId.toString());
        if (PhaseId.RESTORE_VIEW.equals(phaseId)) {
            afterRestoreView();
        } else if (PhaseId.APPLY_REQUEST_VALUES.equals(phaseId)) {
            afterApplyRequestValues();
        } else if (PhaseId.PROCESS_VALIDATIONS.equals(phaseId)) {
            afterProcessValidations();
        } else if (PhaseId.UPDATE_MODEL_VALUES.equals(phaseId)) {
            afterUpdateModelValues();
        } else if (PhaseId.INVOKE_APPLICATION.equals(phaseId)) {
            afterInvokeApplication();
        } else if ((PhaseId.RENDER_RESPONSE.equals(phaseId))
                && (!FacesContext.getCurrentInstance().getResponseComplete())) {
            afterRenderResponse();
        }
    }

    protected void beforeRestoreView() {
    }

    protected void afterRestoreView() {
    }

    protected void beforeApplyRequestValues() {
    }

    protected void afterApplyRequestValues() {
    }

    protected void beforeProcessValidations() {
    }

    protected void afterProcessValidations() {
    }

    protected void beforeUpdateModelValues() {
    }

    protected void afterUpdateModelValues() {
    }

    protected void beforeInvokeApplication() {
    }

    protected void afterInvokeApplication() {
    }

    protected void beforeRenderResponse() {
    }

    protected void afterRenderResponse() {
    }

    protected boolean isPostBack() {
        return FacesUtils.isPostBack();
    }

    protected void renderResponse() {
        FacesUtils.renderResponse();
    }

    protected void responseComplete() {
        FacesUtils.responseComplete();
    }

    public void init() {
    }

    public void preprocess() {
    }

    public void prerender() {
    }

    public void destroy() {
    }
}
